class UnionFind:
    def __init__(self):
        self.parent = {}
        self.count = 0

    def find(self, x):
        if x not in self.parent:
            self.parent[x] = x
            self.count += 1

        if x != self.parent[x]:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)

        if root_x == root_y:
            return

        self.parent[root_x] = root_y
        self.count -= 1

    def get_count(self):
        return self.count


class Solution:
    def removeStones(self, stones):
        union_find = UnionFind()
        for stone in stones:

            union_find.union(stone[0] + 10000, stone[1])

        return len(stones) - union_find.get_count()
